package com.orangelife.service.propFeeDirectOffsetInfo.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.github.pagehelper.PageHelper;
import com.orangelife.common.MybaitsGenericDaoSubLogic;
import com.orangelife.exception.AppBizException;
import com.orangelife.service.propFeeDirectOffsetInfo.IPropFeeDirectOffsetInfoService;
import com.orangelife.service.propFeeDirectOffsetInfo.model.PropFeeDirectOffsetInfoDto;
import com.orangelife.setting.constant.CodeConst;
import com.orangelife.setting.constant.InfoConst;
import com.orangelife.setting.constant.TypeConst;
import com.orangelife.utils.StringUtils;
import com.orangelife.utils.UUIDGenerator;

/*****************************************************************
 * Copyright(C) ORANGELIFE Corporation CodeConst.BIS_CODE_68001015. All Rights
 * Reserved.
 *****************************************************************/
/*
 * ============================================================== 功能名
 * PropFeeDirectOffsetInfo类的 service层的增删改查
 * 
 * @author @author zhr
 * 
 * @version 1.1.0
 * 
 * @date 2017-01-13 14:12:22
 * ==============================================================
 */
@Service("propFeeDirectOffsetInfoService")
public class PropFeeDirectOffsetInfoServiceImpl extends
		MybaitsGenericDaoSubLogic implements IPropFeeDirectOffsetInfoService {
	private static Logger logger = Logger
			.getLogger(PropFeeDirectOffsetInfoServiceImpl.class);

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		PropFeeDirectOffsetInfoServiceImpl.logger = logger;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int addPropFeeDirectOffsetInfo(
			PropFeeDirectOffsetInfoDto propFeeDirectOffsetInfo)
			throws AppBizException {

		Assert.notNull(propFeeDirectOffsetInfo);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/

		if (!StringUtils.isBlank(propFeeDirectOffsetInfo.getFeeDate())) {
			if (propFeeDirectOffsetInfo.getFeeDate().length() > 16) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "所属月份长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeDirectOffsetInfo.getOffsetType())) {
			if (propFeeDirectOffsetInfo.getOffsetType().length() > 1) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"1自动,2人工长度超出");
			}
		}

		if (StringUtils.isBlank(propFeeDirectOffsetInfo.getOffsetID())) {
			propFeeDirectOffsetInfo.setOffsetID(UUIDGenerator.nextOrderNum());
		}
		propFeeDirectOffsetInfo.setItemStat(TypeConst.FLAG_ON);
		propFeeDirectOffsetInfo.setTscreate(new Date());
		propFeeDirectOffsetInfo.setTsedit(new Date());
		return this.getSqlSession().insert(
				"PropFeeDirectOffsetInfoMapper.insertSelective",
				propFeeDirectOffsetInfo);
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int editPropFeeDirectOffsetInfo(
			PropFeeDirectOffsetInfoDto propFeeDirectOffsetInfo)
			throws AppBizException {

		Assert.notNull(propFeeDirectOffsetInfo);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/
		if (StringUtils.isBlank(propFeeDirectOffsetInfo.getOffsetID())) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
		PropFeeDirectOffsetInfoDto oldPropFeeDirectOffsetInfo = this
				.findPropFeeDirectOffsetInfoByID(propFeeDirectOffsetInfo
						.getOffsetID());

		if (!StringUtils.isBlank(propFeeDirectOffsetInfo.getFeeDate())) {
			if (propFeeDirectOffsetInfo.getFeeDate().length() > 16) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "所属月份长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeDirectOffsetInfo.getOffsetType())) {
			if (propFeeDirectOffsetInfo.getOffsetType().length() > 1) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"1自动,2人工长度超出");
			}
		}

		if (oldPropFeeDirectOffsetInfo == null) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}
		propFeeDirectOffsetInfo.setTsedit(new Date());
		return this.getSqlSession().update(
				"PropFeeDirectOffsetInfoMapper.updateByPrimaryKeySelective",
				propFeeDirectOffsetInfo);
	}

	@Override
	public PropFeeDirectOffsetInfoDto findPropFeeDirectOffsetInfoByID(
			String propFeeDirectOffsetInfoID) throws AppBizException {
		if (StringUtils.isBlank(propFeeDirectOffsetInfoID)) {

			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}

		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		PropFeeDirectOffsetInfoDto oldPropFeeDirectOffsetInfo = this
				.getSqlSession()
				.selectOne(
						"PropFeeDirectOffsetInfoSearchMapper.selectByPrimaryKey",
						propFeeDirectOffsetInfoID);
		if (oldPropFeeDirectOffsetInfo == null) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}

		if (TypeConst.FLAG_OFF.equals(oldPropFeeDirectOffsetInfo.getItemStat())) {

			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}
		return oldPropFeeDirectOffsetInfo;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int deletePropFeeDirectOffsetInfoByLogic(
			String propFeeDirectOffsetInfoID) throws AppBizException {

		if (StringUtils.isBlank(propFeeDirectOffsetInfoID)) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		PropFeeDirectOffsetInfoDto oldPropFeeDirectOffsetInfo = this
				.findPropFeeDirectOffsetInfoByID(propFeeDirectOffsetInfoID);

		oldPropFeeDirectOffsetInfo.setItemStat(TypeConst.FLAG_OFF);
		oldPropFeeDirectOffsetInfo.setTsedit(new Date());
		return this.getSqlSession().update(
				"PropFeeDirectOffsetInfoMapper.updateByPrimaryKeySelective",
				oldPropFeeDirectOffsetInfo);
	}

	@Override
	public List<Map<String, Object>> findPropFeeDirectOffsetInfoByPage(
			String tsCreate, String search, int pageNo, int pageSize)
			throws AppBizException {

		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tsCreate", tsCreate);
		map.put("search", search);
		PageHelper.startPage(pageNo, pageSize);
		List<Map<String, Object>> list = this
				.getSqlSession()
				.selectList(
						"PropFeeDirectOffsetInfoSearchMapper.findAllPropFeeDirectOffsetInfo",
						map);
		return list;
	}

	@Override
	public List<PropFeeDirectOffsetInfoDto> getPropFeeDirectOffsetInfoByPage(
			String tsCreate, String search, int pageNo, int pageSize)
			throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tsCreate", tsCreate);
		map.put("search", search);
		PageHelper.startPage(pageNo, pageSize);
		List<PropFeeDirectOffsetInfoDto> list = this
				.getSqlSession()
				.selectList(
						"PropFeeDirectOffsetInfoSearchMapper.getAllPropFeeDirectOffsetInfoDto",
						map);
		return list;
	}

	@Override
	public List<Map<String, Object>> selectPropFeeDirectOffsetInfoByRelevantID(
			String offsetID, String commID, String apartmentID,
			String feeDetailID, String feeTypeID, String offsetType,
			String orderID) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("offsetID", offsetID);
		map.put("commID", commID);
		map.put("apartmentID", apartmentID);
		map.put("feeDetailID", feeDetailID);
		map.put("feeTypeID", feeTypeID);
		map.put("offsetType", offsetType);
		map.put("orderID", orderID);

		List<Map<String, Object>> list = this
				.getSqlSession()
				.selectList(
						"PropFeeDirectOffsetInfoSearchMapper.selectPropFeeDirectOffsetInfoDtoyRelevantID",
						map);
		return list;
	}

	@Override
	public List<PropFeeDirectOffsetInfoDto> findPropFeeDirectOffsetInfoByRelevantID(
			String offsetID, String commID, String apartmentID,
			String feeDetailID, String feeTypeID, String offsetType,
			String orderID) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("offsetID", offsetID);
		map.put("commID", commID);
		map.put("apartmentID", apartmentID);
		map.put("feeDetailID", feeDetailID);
		map.put("feeTypeID", feeTypeID);
		map.put("offsetType", offsetType);
		map.put("orderID", orderID);

		List<PropFeeDirectOffsetInfoDto> list = this
				.getSqlSession()
				.selectList(
						"PropFeeDirectOffsetInfoSearchMapper.findPropFeeDirectOffsetInfoDtoByRelevantID",
						map);
		return list;
	}

	@Override
	public List<PropFeeDirectOffsetInfoDto> bauthfindPropFeeDirectOffsetInfo(
			List<String> propFeeDirectOffsetInfoIDs) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<PropFeeDirectOffsetInfoDto> list = this
				.getSqlSession()
				.selectList(
						"PropFeeDirectOffsetInfoSearchMapper.batchFindPropFeeDirectOffsetInfo",
						propFeeDirectOffsetInfoIDs);
		return list;
	}

	@Override
	public List<Map<String, Object>> bauthfindPropFeeDirectOffsetInfoByMap(
			Map<String, Object> propFeeDirectOffsetInfo) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<Map<String, Object>> list = this
				.getSqlSession()
				.selectList(
						"PropFeeDirectOffsetInfoSearchMapper.batchFindPropFeeDirectOffsetInfoByMap",
						propFeeDirectOffsetInfo);
		return list;
	}

	@Override
	public void clearCache() throws AppBizException {
		System.out.println("清除缓存成功");
	}

	@Override
	public List<Map<String, Object>> synchDirBillOffData(Map<String, Object> map)
			throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		if (map.containsKey("tsedit")) {
			if (map.get("tsedit") != null) {
				if (!StringUtils.isInt(map.get("tsedit").toString())) {
					throw new AppBizException(2, "参数类型错误");
				}
				map.put("tsedit",
						Long.parseLong(map.get("tsedit").toString()) / 1000);
			}
		}
		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeDirectOffsetInfoSearchMapper.synchDirBillOffData", map);
		return list;
	}

}